package com.fivefu.core.hik.services.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hik.dto.response.TColsnote;
import com.fivefu.core.hik.dto.response.THikTaxiData;
import com.fivefu.core.hik.entity.DbHikVehicle;
import com.fivefu.core.hik.mapper.DbHikVehicleMapper;
import com.fivefu.core.hik.services.DbHikVehicleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.module.dictionary.entity.DbSysDict;
import com.fivefu.module.dictionary.service.DbSysDictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 海康车辆信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-01 18:02:39
 */
@Service
public class DbHikVehicleServiceImpl extends ServiceImpl<DbHikVehicleMapper, DbHikVehicle> implements DbHikVehicleService {

    @Resource
    private DbSysDictService dbSysDictService;

    /**
     * 获取所有车辆信息
     * @return
     */
    @Override
    public List<DbHikVehicle> getVehicleInformation() {
        QueryWrapper<DbHikVehicle> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<DbHikVehicle> list = baseMapper.selectList(wrapper);
        return list;
    }

    /**
     * 获取所有车辆信息的设备与车辆编码列表
     * @return
     */
    @Override
    public List<Map<String, String>> getVehicleInformationMap() {
        List<Map<String, String>> listMap = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        QueryWrapper<DbHikVehicle> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<DbHikVehicle> list = baseMapper.selectList(wrapper);
        for (DbHikVehicle vehicle : list) {//vehicleIndexCode
            map = new HashMap<>();
            String vehicleColor = vehicle.getVehicleIndexCode();
            List<Map> deviceIndexCodeList = JSONArray.parseArray(vehicle.getDeviceResourceDTOList(), Map.class);
            for (Map deviceMap : deviceIndexCodeList) {
                Object deviceIndexCode = deviceMap.get("deviceIndexCode");
                map.put("deviceIndexCode", String.valueOf(deviceIndexCode));
            }
            map.put("vehicleIndexCode", vehicleColor);
            listMap.add(map);
        }
        return listMap;
    }

    /**
     * 根据车牌号查询车辆信息id
     * @param vehicleLicensePlate 车牌号
     * @return 返回这条数据的id
     */
    @Override
    public Long getVehicle(String vehicleLicensePlate) {
        QueryWrapper<DbHikVehicle> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("plate_no",vehicleLicensePlate);
        DbHikVehicle vehicle = baseMapper.selectOne(wrapper);
        Long id = vehicle.getId();
        return id;
    }

    /**
     *
     * @param vehicle 海康车辆表对象
     */
    @Override
    public void editHikVehicle(DbHikVehicle vehicle) {

        //判断车牌颜色是否为空
        if (StrUtils.isNotNull(vehicle.getPlateColor())) {
            DbSysDict dict = dbSysDictService.queryDictByValue("LPC", vehicle.getPlateColor());
            if(dict != null){
                vehicle.setPlateColor(dict.getDictCode());
            }
        }

        //判断车身颜色是否为空
        if (StrUtils.isNotNull(vehicle.getVehicleColor())) {
            DbSysDict dict = dbSysDictService.queryDictByValue("BC", vehicle.getVehicleColor());
            if(dict != null){
                vehicle.setVehicleColor(dict.getDictCode());
            }
        }

        //判断运输类型是否为空
        if (StrUtils.isNotNull(vehicle.getTransportType())) {
            DbSysDict dict = dbSysDictService.queryDictByValue("TPT", vehicle.getTransportType());
            if(dict != null){
                vehicle.setTransportType(dict.getDictCode());
            }
        }

        //判断车辆类型是否为空
        if (StrUtils.isNotNull(vehicle.getVehicleType())) {
            DbSysDict dict = dbSysDictService.queryDictByValue("VT", vehicle.getVehicleType());
            if(dict != null){
                vehicle.setVehicleType(dict.getDictCode());
            }
        }

        //判断燃料类型是否为空
        if (StrUtils.isNotNull(vehicle.getFuelType())) {
            DbSysDict dict = dbSysDictService.queryDictByValue("FT", vehicle.getFuelType());
            if(dict != null){
                vehicle.setFuelType(dict.getDictCode());
            }
        }

        //车辆监控点关联id不为空则为更新
        if (null != vehicle.getId()) {
            baseMapper.updateById(vehicle);
        } else {
            baseMapper.insert(vehicle);
        }
    }

    @Override
    public Map getTaxiData(String status, String plateNo) {
        Map<String, List> map = new HashMap<>();
        List<TColsnote> colsnotesList = new ArrayList<>();
        TColsnote tColsnote = new TColsnote();
        tColsnote.setColnote("id");
        tColsnote.setColname("id");
        colsnotesList.add(tColsnote);
        TColsnote tColsnote1 = new TColsnote();
        tColsnote1.setColnote("是否删除0否1是");
        tColsnote1.setColname("isDelete");
        colsnotesList.add(tColsnote1);
        TColsnote tColsnote2 = new TColsnote();
        tColsnote2.setColnote("创建人");
        tColsnote2.setColname("createdBy");
        colsnotesList.add(tColsnote2);
        TColsnote tColsnote3 = new TColsnote();
        tColsnote3.setColnote("创建时间");
        tColsnote3.setColname("createdTime");
        colsnotesList.add(tColsnote3);
        TColsnote tColsnote4 = new TColsnote();
        tColsnote4.setColnote("更新人");
        tColsnote4.setColname("updatedBy");
        colsnotesList.add(tColsnote4);
        TColsnote tColsnote5 = new TColsnote();
        tColsnote5.setColnote("更新时间");
        tColsnote5.setColname("updatedTime");
        colsnotesList.add(tColsnote5);
        TColsnote tColsnote6 = new TColsnote();
        tColsnote6.setColnote("备注");
        tColsnote6.setColname("remark");
        colsnotesList.add(tColsnote6);
        TColsnote tColsnote7 = new TColsnote();
        tColsnote7.setColnote("车辆编号;唯一值编码");
        tColsnote7.setColname("vehicleIndexCode");
        colsnotesList.add(tColsnote7);
        TColsnote tColsnote8 = new TColsnote();
        tColsnote8.setColnote("车辆自编号");
        tColsnote8.setColname("selfNo");
        colsnotesList.add(tColsnote8);
        TColsnote tColsnote9 = new TColsnote();
        tColsnote9.setColnote("车辆状态");
        tColsnote9.setColname("status");
        colsnotesList.add(tColsnote9);
        TColsnote tColsnote10 = new TColsnote();
        tColsnote10.setColnote("车牌号码");
        tColsnote10.setColname("plateNo");
        colsnotesList.add(tColsnote10);
        TColsnote tColsnote11 = new TColsnote();
        tColsnote11.setColnote("车牌颜色");
        tColsnote11.setColname("plateColor");
        colsnotesList.add(tColsnote11);
        TColsnote tColsnote12 = new TColsnote();
        tColsnote12.setColnote("区域编号");
        tColsnote12.setColname("regionIndexCode");
        colsnotesList.add(tColsnote12);
        TColsnote tColsnote13 = new TColsnote();
        tColsnote13.setColnote("车身颜色");
        tColsnote13.setColname("vehicleColor");
        colsnotesList.add(tColsnote13);
        TColsnote tColsnote14 = new TColsnote();
        tColsnote14.setColnote("运输类型");
        tColsnote14.setColname("transportType");
        colsnotesList.add(tColsnote14);
        TColsnote tColsnote15 = new TColsnote();
        tColsnote15.setColnote("车辆类型");
        tColsnote15.setColname("vehicleType");
        colsnotesList.add(tColsnote15);
        TColsnote tColsnote16 = new TColsnote();
        tColsnote16.setColnote("燃料类型");
        tColsnote16.setColname("fuelType");
        colsnotesList.add(tColsnote16);
        TColsnote tColsnote17 = new TColsnote();
        tColsnote17.setColnote("业户名称");
        tColsnote17.setColname("ownerName");
        colsnotesList.add(tColsnote17);
        TColsnote tColsnote18 = new TColsnote();
        tColsnote18.setColnote("业户编码");
        tColsnote18.setColname("ownerCode");
        colsnotesList.add(tColsnote18);
        TColsnote tColsnote19 = new TColsnote();
        tColsnote19.setColnote("主设备编号");
        tColsnote19.setColname("primaryDeviceIndexCode");
        colsnotesList.add(tColsnote19);
        TColsnote tColsnote20 = new TColsnote();
        tColsnote20.setColnote("从设备编号");
        tColsnote20.setColname("subDeviceIndexCode");
        colsnotesList.add(tColsnote20);
        TColsnote tColsnote21 = new TColsnote();
        tColsnote21.setColnote("驾驶员编号");
        tColsnote21.setColname("driverIndexCode");
        colsnotesList.add(tColsnote21);
        TColsnote tColsnote22 = new TColsnote();
        tColsnote22.setColnote("关联驾驶员集合");
        tColsnote22.setColname("driverResourceDTOList");
        colsnotesList.add(tColsnote22);
        TColsnote tColsnote23 = new TColsnote();
        tColsnote23.setColnote("关联设备集合");
        tColsnote23.setColname("deviceResourceDTOList");
        colsnotesList.add(tColsnote23);
        TColsnote tColsnote24 = new TColsnote();
        tColsnote24.setColnote("车辆经纬度");
        tColsnote24.setColname("shapestr");
        colsnotesList.add(tColsnote24);
        TColsnote tColsnote25 = new TColsnote();
        tColsnote25.setColnote("监控编码");
        tColsnote25.setColname("cameracode");
        colsnotesList.add(tColsnote25);
        List<THikTaxiData> taxiData = baseMapper.getTaxiData(status, plateNo);
        map.put("colsnote",colsnotesList);
        map.put("data",taxiData);
        return map;
    }

}
